| 1 | import {
|
| 2 | View,
|
| 3 | StyleSheet,
|
| 4 | KeyboardAvoidingView,
|
| 5 | ScrollView,
|
| 6 | } from "react-native";
|
| 7 | import { Text } from "@/components/ui/Text";
|
| 8 | import { useState, useEffect } from "react";
|
| 9 | import { router, useLocalSearchParams, Stack } from "expo-router";
|
| 10 |
|
| 11 | import { Card } from "@/components/ui/Cards";
|
| 12 | import { TextArea } from "@/components/ui/Input";
|
| 13 | import { Button } from "@/components/ui/Button";
|
| 14 | import { Spacing, Typography, Colors, BorderRadius } from "@/constants/theme";
|
| 15 | import { Ionicons } from "@expo/vector-icons";
|
| 16 | import { DatePickerField } from "@/components/ui/DatePickerField";
|
| 17 | import { ScaleSlider } from "@/components/ui/ScaleSlider";
|
| 18 | import { useSleepRecord, useUpdateSleepRecord } from "@/hooks";
|
| 19 | import { parseDateOnly } from "@/lib/utils/time";
|
| 20 | import { ActivityIndicator, View as ActivityView } from "react-native";
|
| 21 |
|
| 22 | export default function EditSleepRecord() {
|
| 23 | const { id } = useLocalSearchParams<{ id: string }>();
|
| 24 | const { data: sleepRecord, isLoading } = useSleepRecord(id);
|
| 25 | const updateSleepRecord = useUpdateSleepRecord();
|
| 26 |
|
| 27 | const [date, setDate] = useState(new Date());
|
| 28 | const [annotations, setAnnotations] = useState("");
|
| 29 | const [average, setAverage] = useState(7.5);
|
| 30 |
|
| 31 | useEffect(() => {
|
| 32 | if (sleepRecord) {
|
| 33 | setDate(parseDateOnly(sleepRecord.date));
|
| 34 | setAnnotations(sleepRecord.annotations || "");
|
| 35 | setAverage(sleepRecord.average);
|
| 36 | }
|
| 37 | }, [sleepRecord]);
|
| 38 |
|
| 39 | const formatValue = (value: number): string => {
|
| 40 | return String(value).replaceAll(".", ",");
|
| 41 | };
|
| 42 |
|
| 43 | const save = async () => {
|
| 44 | const data = {
|
| 45 | date,
|
| 46 | annotations,
|
| 47 | average,
|
| 48 | };
|
| 49 |
|
| 50 | await updateSleepRecord.mutateAsync({ id: String(id), payload: data });
|
| 51 | router.back();
|
| 52 | };
|
| 53 |
|
| 54 | if (isLoading) {
|
| 55 | return (
|
| 56 | <ActivityView style={styles.centered}>
|
| 57 | <ActivityIndicator size="large" color={Colors.light.tint} />
|
| 58 | </ActivityView>
|
| 59 | );
|
| 60 | }
|
| 61 |
|
| 62 | return (
|
| 63 | <KeyboardAvoidingView behavior="height" style={styles.keyboardView}>
|
| 64 | <Stack.Screen options={{ title: "Editar Registro" }} />
|
| 65 | <ScrollView scrollEventThrottle={16}>
|
| 66 | <View style={styles.container}>
|
| 67 | <View style={styles.header}>
|
| 68 | <View style={styles.innerHeaderWrapper}>
|
| 69 | <View style={[styles.headerIcon, { backgroundColor: "#EFF6FF" }]}>
|
| 70 | <Ionicons name="moon-outline" size={20} color="#2563EB" />
|
| 71 | </View>
|
| 72 |
|
| 73 | <View style={styles.headerTitleSubtitle}>
|
| 74 | <Text style={styles.headerTitle}>Editar Sono</Text>
|
| 75 | <Text style={styles.headerSubtitle}>
|
| 76 | Atualize os dados do sono
|
| 77 | </Text>
|
| 78 | </View>
|
| 79 | </View>
|
| 80 |
|
| 81 | <Text style={styles.description}>
|
| 82 | Você pode editar este registro nos primeiros 5 minutos após a
|
| 83 | criação.
|
| 84 | </Text>
|
| 85 | </View>
|
| 86 |
|
| 87 | <Card style={styles.mainCard}>
|
| 88 | <DatePickerField
|
| 89 | label="Data do Registro"
|
| 90 | initialDate={date}
|
| 91 | maximumDate={new Date()}
|
| 92 | onChange={setDate}
|
| 93 | />
|
| 94 |
|
| 95 | <View style={{ marginVertical: 15 }}>
|
| 96 | <ScaleSlider
|
| 97 | label="Duração"
|
| 98 | value={average}
|
| 99 | onValueChange={setAverage}
|
| 100 | onValueFormat={formatValue}
|
| 101 | step={0.5}
|
| 102 | min={0.0}
|
| 103 | max={15.0}
|
| 104 | minLabel="0h"
|
| 105 | maxLabel="15h"
|
| 106 | />
|
| 107 | </View>
|
| 108 |
|
| 109 | <TextArea
|
| 110 | label="Notas sobre a qualidade"
|
| 111 | variant="darkGhost"
|
| 112 | onChangeText={(val) => setAnnotations(val)}
|
| 113 | value={annotations}
|
| 114 | minRows={5}
|
| 115 | maxRows={10}
|
| 116 | placeholder="Como você se sentiu ao acordar? Teve sonhos marcantes?"
|
| 117 | />
|
| 118 | </Card>
|
| 119 |
|
| 120 | <Button
|
| 121 | title="Salvar Alterações"
|
| 122 | onPress={save}
|
| 123 | loading={updateSleepRecord.isPending}
|
| 124 | disabled={updateSleepRecord.isPending}
|
| 125 | />
|
| 126 | </View>
|
| 127 | </ScrollView>
|
| 128 | </KeyboardAvoidingView>
|
| 129 | );
|
| 130 | }
|
| 131 |
|
| 132 | const styles = StyleSheet.create({
|
| 133 | container: {
|
| 134 | gap: 24,
|
| 135 | paddingHorizontal: Spacing.containerPadding,
|
| 136 | paddingVertical: Spacing.sectionGap,
|
| 137 | },
|
| 138 | mainCard: {
|
| 139 | padding: Spacing.cardGap,
|
| 140 | },
|
| 141 | header: {},
|
| 142 | headerIcon: {
|
| 143 | paddingVertical: 15,
|
| 144 | paddingHorizontal: 14,
|
| 145 | width: 48,
|
| 146 | borderRadius: BorderRadius.md,
|
| 147 | },
|
| 148 | innerHeaderWrapper: {
|
| 149 | flex: 1,
|
| 150 | flexDirection: "row",
|
| 151 | alignItems: "center",
|
| 152 | },
|
| 153 | headerTitleSubtitle: {
|
| 154 | marginLeft: 10,
|
| 155 | },
|
| 156 | headerTitle: {
|
| 157 | ...Typography.headlineMd,
|
| 158 | },
|
| 159 | headerSubtitle: {
|
| 160 | ...Typography.bodyMd,
|
| 161 | color: Colors.light.textSecondary,
|
| 162 | },
|
| 163 | description: {
|
| 164 | ...Typography.bodyMd,
|
| 165 | color: Colors.light.textSecondary,
|
| 166 | marginTop: 20,
|
| 167 | },
|
| 168 | keyboardView: {
|
| 169 | flex: 1,
|
| 170 | },
|
| 171 | centered: {
|
| 172 | flex: 1,
|
| 173 | justifyContent: "center",
|
| 174 | alignItems: "center",
|
| 175 | },
|
| 176 | });
|